home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / basic / tutorqb.com / TUTOR-02.BAS < prev    next >
Encoding:
BASIC Source File  |  1986-10-07  |  14.3 KB  |  314 lines

  1. goto example1
  2. '                       The QuickBASIC Tutorial Series
  3.  
  4. '                                   Part 2
  5. '                         Introduction to Programming
  6.  
  7. '                        Copyright 1986  Ford Software
  8.  
  9. '                            4845 Willowbend Blvd.
  10. '                              Houston, TX 77035
  11. '                                (713) 721-5205
  12. '                          CompuServe ppn: 71355,470
  13.  
  14. 'This Tutorial may not be copied or distributed in any form - printed, on disk
  15. 'or electronically - without the express written permission of Ford Software.
  16. '          Unlicensed distribution is a violation of copyright law
  17. '           and may be subject to civil and criminal prosecution.
  18.  
  19. 'QuickBASIC is a trademark of the Microsoft Corporation.
  20. 'Ford Software is not associated in any way with the Microsoft Corporation.
  21.  
  22. '(Make sure NumLock is off and press PgDn)
  23. 'page 2                     I N T R O D U C T I O N
  24.  
  25. 'In this part of the QB2 tutorial, we are going to assume that you have never
  26. 'programmed before. The QuickBASIC manual is primarily a reference book, not
  27. 'a book to learn by. The value of this tutorial is that it is interactive
  28. 'You will be able to try things out as you go, which greatly speeds up the
  29. 'learning process. However, for maximum benefit, as points are covered here,
  30. 'you should also read about them in the QB2 manaul.
  31.  
  32. 'Here are some basics you will need to start with:
  33. ' - When entering code, you can put each statement on a separate line, or you
  34. 'can combine several on the same line with a colon (:) in between statements.
  35. ' - The compile ignores anything following a single quote mark (').
  36. ' - In your programs, you will constantly be testing the relationship between
  37. 'things. Here are the relational symbols. "AND" and "OR" can be used to make
  38. 'multiple tests.          =        equal to
  39. '                         <        less than
  40. '                         >        greater than
  41. '                        <>        not equal to
  42. '                    =<  or  <=    less than or equal to
  43. '                    =>  or  >=    greater than or equal to
  44. '(Press PgDn)
  45. 'page 3
  46. '                                 Contents
  47. '                 page
  48. '                  4      What's a Program
  49. '                  4      What's a Variable
  50. '                  5      Integer Variables
  51. '                  6      Single Precision Variables
  52. '                  6      Double Precision Variables
  53. '                  6      String Variables
  54. '                  7      Declaring Variable Types
  55. '                  7      Review
  56. '                  8      Examples of Variable Types
  57. '                  9      Arrays
  58. '                  9      Loops
  59. '                 10      FOR-NEXT Loops
  60. '                 11      WHILE-WEND Loops
  61. '                 12      String Handling Commands
  62. '                 13      String Operators
  63. '                 14      Other String Commands
  64. '
  65. '                To search for specific text, use Ctrl-F.
  66. '(Press PgDn)
  67. 'page 4                 What's a Program, a Variable?
  68.  
  69. 'WHAT'S A PROGRAM?
  70. 'A program is a set of instructions to the computer. The compiler converts
  71. 'your English-like instructions to machine language the computer can under-
  72. 'stand. With any computer language, such as QB2, you must use the "vocabulary"
  73. 'and "sentence structure" of the language exactly as proscribed.
  74.  
  75. 'In movies and TV shows, the hero sits at the computer and types something
  76. 'like "PRINT NAMES OF ALL PEOPLE WHO EAT QUICHE" and the computer scrolls a
  77. 'list across the screen, usually a letter at a time with an electronic beep
  78. 'as each letter is printed. Ah well, that's showbiz.
  79.  
  80. 'In the real world of QB2, if you enter something like the line above, the
  81. 'computer will quickly and quietly display "0 0 0 0 0 0 0", because it thinks
  82. 'each of those words ("NAMES" through "QUICHE") is a numeric variable with a
  83. 'value of 0 which you have asked it to print.
  84.  
  85. 'WHAT'S A VARIABLE?
  86. 'A variable is used to store data while a program is running. A variable can
  87. 'be almost any combination of letters and numbers, but if you want to be able
  88. '   (Press PgDn)
  89. 'page 5                         Variable Types
  90.  
  91. 'to remember when you look back at your program later what a variable is for,
  92. 'it's a good idea to use a variable name that will remind you.
  93.  
  94. Example1:
  95. INPUT "What were total sales"; SALES
  96. INPUT "What is the sales tax rate"; TAX.RATE
  97. PRINT "Sales tax on sales of" SALES "is" SALES * TAX.RATE
  98. END'                                       variables 
  99.  
  100. 'To run this example, just press Ctrl-R. The program should compile and then
  101. 'when you press Enter, it should run. If it does not, press Alt-F, L, Enter,
  102. 'and reload the program from the disk. If it still does not, go back to DOS
  103. 'and recopy the program from the distribution disk onto your work disk again.
  104.  
  105. 'INTEGER VARIABLES...
  106. '   can hold numbers from -32,768 to +32,767 with no decimal places. Integers
  107. 'take up the least amount of space in memory, which makes them faster to
  108. 'manipulate as well. The sign for an integer is "%".  Eg:  X%=3
  109.  
  110. '(PgDn)
  111. 'page 6                   (Variable Types, Continued)
  112.  
  113. 'SINGLE PRECISION VARIABLES...
  114. '   are precise to 7 digits total, on both sides of the decimal place. For
  115. 'example, 1234567 and 1234.567 are both single precision numbers.  Beyond
  116. 'seven digits you start getting rounding errors. Single precision numbers take
  117. 'twice as much space as integers, but only half as much as double precision
  118. 'numbers. You always want to use the smallest varible type that will hold the
  119. 'number accurately. A single precision variable is denoted with a "!":  X!=6.2
  120.  
  121. 'DOUBLE PRECISION VARIABLES...
  122. '   are accurate to 16 digits, which is the most accuracy you can squeeze
  123. 'out of the system in BASIC. Like single precision, the number of digits is
  124. 'the total on both sides of the decimal place. A double precision variable is
  125. 'indicated with a "#".  Eg:  X#=123456.789
  126.  
  127. 'STRING VARIABLES
  128. 'A string variable holds text. In the interpreter a string variable is limited
  129. 'to 255 characters. In the compiler, a string variable may hold up to 32,767
  130. 'characters. The symbol for a string variable is a "$".  Eg:  X$="Hello"
  131.  
  132. '(PgDn)
  133. 'page 7                    Declaring Variable Types
  134.  
  135. 'DECLARING VARIABLE TYPES
  136. 'It is a nuisance to have to keep typing the symbol for the variable type
  137. 'every time you enter a variable name. The DEFINT, DEFSNG, DEFDBL and DEFSTR
  138. 'commands let you establish that any variable name starting with the specified
  139. 'letter is a particular type of variable. For example, DEFDBL S means that any
  140. 'variable starting with the letter S is a double precision variable. This
  141. 'declaration can be overridden by using the symbol. For example, SALES would
  142. 'be a double-precision variable and SONG$ would be a string variable.
  143.  
  144. 'REVIEW
  145. ' X% is an integer that can hold numbers from -32,768 to +32,767, no decimals.
  146. ' X! is single precision and can hold numbers accurately to 7 digits.
  147. ' X# is double precision and accurate to 16 digits.
  148. ' X$ is a string variable that can hold up to 32,767 characters.
  149.  
  150. 'PROGRAMMING TIP
  151. 'At the start of a program, you can declare all variables as integers and use
  152. 'the appropriate symbols to override this declaration for other variable types
  153. 'only when needed. This insures that you will use the faster, smaller integers
  154. 'whenever possible.                   (PgDn)
  155. 'page 8                   Examples of Variable Types
  156.  
  157. Example2:   'Remove the quote from the start of lines 3 and 4 before running.
  158. DEFINT A-Z               'To run this example, start by pressing Ctrl-PgUp to
  159. A=123: PRINT A           'get to the first line of this file. Edit that line
  160. 'B=123456: PRINT B       'to say "goto example2" and press Ctrl-R to run.
  161. 'C=12345675: PRINT C     'You will get a compile-time error message saying
  162. END                      '"Math overflow" for the preceeding line. Change the
  163. 'C's in that line to C# and press Ctrl-R. (You don't have to change the first
  164. 'line in the file again.) You still get the same error message. What's going
  165. 'on? Well, QB2 does not always point to the right line. The line above is also
  166. 'wrong. Change the B's in that line to B! and try again.
  167.  
  168. 'Now let's try some cutting and pasting. Move to the first D in the next line:
  169. 'D="123456abc": PRINT D '(Press Alt-V, E, Enter to remove error message box.)
  170. 'With the NumLock off, press Shift-End. The line above should be highlighted.
  171. 'Press the Del key.  Move the cursor to the "E" in the "END" statement in
  172. 'Example2 above and press Enter. Move up a row to the blank line and press
  173. 'the Ins key and the text you "cut" out should reappear. Press Ctrl-R to run
  174. 'this modified code. You will get a "Data type conflict" for trying to assign
  175. 'a text string to a non-string variable. Add the "$" symbols and try again.
  176. '(PgDn)
  177. 'page 9                         Arrays, Loops
  178.  
  179. 'ARRAYS...
  180. '  are what makes variables and programs really powerful. Thanks to spread-
  181. 'sheet programs, we now have a good analogy for arrays: rows and columns in
  182. 'the spreadsheet. A single array is like a single column of data in a spread-
  183. 'sheet. A two-element array is like multiple columns and rows.  An example of
  184. 'a variable array would be SALES#(12,50) where the 12 would be the number of
  185. 'columns in a spreadsheet, perhaps a column for each month, and 50 would be
  186. 'the number of rows, perhaps a product number on each row.
  187.  
  188. 'LOOPS...
  189. '   are what give arrays their power (and vice-versa).  Let's say you wanted
  190. 'to increase sales for each item in a column by 20%. In a spreadsheet, you
  191. 'would have to go down the column multiplying each of the 50 amounts by 120%.
  192. 'With a FOR-NEXT loop in BASIC, you could just say
  193. 'FOR ROW=1 TO 50
  194. '  SALES#(COLUMN,ROW)=SALES#(COLUMN,ROW) * 1.2    'Some people get confused
  195. 'NEXT    ' by the structure "x=x+3" because it is algebraically impossible.
  196.      ' This is not algebra. This is shorthand for a language which says
  197.      '         "set  x  equal to the old value of  x  plus  3"
  198. '(PgDn)
  199. 'page 10                        FOR-NEXT Loops
  200.  
  201. Example3:
  202. FOR I=6 TO 9    ' This example tells the computer to set the value of I to
  203.   PRINT "I="I   ' the value of the first number (6), then perform the action
  204. NEXT            ' if I is less than or equal to the second number (9).  I is
  205. 'incremented by 1 when NEXT is reached and tested again to see if it is still
  206. 'less than or equal to 9.
  207.  
  208. FOR J=1 TO 8 STEP 3   ' You can also make the counter increment more than 1
  209.   PRINT "J step" J    ' by using the STEP command. In this example, J will
  210. NEXT                  ' increment by 3.
  211.  
  212. FOR K=5 TO 3 STEP -1  ' You can also make the counter decrease instead of
  213.   PRINT "K loop" K    ' counting up by using a negative STEP amount. Here,
  214. NEXT                  ' K will start at 5 and decrement by 1.
  215. END
  216.  
  217. 'To run these examples, press Ctrl-PgUp to get to the title screen, change the
  218. 'first line to "goto example3" and press Ctrl-R. Then return to this page.
  219.  
  220. ' (PgDn)
  221. 'page 11                       WHILE-WEND Loops
  222.  
  223. 'WHILE-WEND LOOPS...
  224. '  repeat an action as long as the testing statment is true. In simplest form,
  225. 'a WHILE-WEND loop can be directly substituted for a FOR-NEXT loop:
  226.  
  227. example4:
  228. I=6              ' Press PgUp to compare this loop to the first loop on the
  229. WHILE I <= 9     ' previous screen. It has exactly the same result, although
  230.   PRINT "I=" I   ' this method is quite a bit more cumbersome. For one thing,
  231.   I=I+1          ' a FOR-NEXT loop increments the counter automatically. In
  232. WEND             ' the WHILE-WEND loop, if you want a counter, you must set
  233. END              ' it up and increment it yourself.
  234.  
  235. 'WHILE-WEND loops are usually more efficient then FOR-NEXT loops when dealing
  236. 'with string variables, but we need to talk more about string variables first.
  237. 'In a textbook, we might have to go through a lot to illustrate the string-
  238. 'handling capabilities of BASIC. In this tutorial, however, all you have to do
  239. 'is press Ctrl-PgUp, change the line to "goto example5" and press Ctrl-R.
  240. '(Example 5 is on the next page.)
  241.  
  242. '(PgDn)
  243. 'page 12                   String-Handling Commands
  244.  
  245. Example5:
  246. X$="QuickBASIC Compiler"
  247. PRINT "                     " X$
  248. PRINT "left 5 characters:   " LEFT$(X$, 5)
  249. PRINT "left 10 characters:  " LEFT$(X$,10)
  250. PRINT "6th thru 10th char.: " MID$(X$,6,5)  'start at char. 6, print 5 char.
  251. PRINT "6th thru last char.: " MID$(X$,6)    'start at char. 6, print the rest
  252. PRINT "right 5 characters:  " RIGHT$(X$,5)
  253. END
  254.  
  255. 'Now here is an example of how a WHILE-WEND loop can be used with a string.
  256. 'We are going to look for someone's last name in this next example:
  257. Example6:
  258. WHOLE.NAME$="Nelson Ford":I=1           'Ctrl-PgUp, "goto example6", Ctrl-R
  259. WHILE MID$(WHOLE.NAME$,I,1) <> " "      'to run this example.
  260.   I=I+1
  261. WEND
  262. PRINT "Last name: " MID$(WHOLE.NAME$,I+1)
  263. END
  264. '      (PgDn)
  265. 'page 13                       String Operators
  266.  
  267. 'String Operators:
  268. ' Symbol        Meaning                Example
  269. '   <    precedes alphabetically      "A" < "B"
  270. '   >    follows alphabetically       "AZ > "AX"
  271. '   =    equals                        X$ = "A"
  272. '  <>    not equal                    "A" <> "a"
  273. '   +    join two strings together     X$ = "A"+"B"
  274.  
  275. 'You can change just part of string by using MID$:
  276. example7:
  277. A$="I think IBM PCs are great."
  278. B$=" Clones"        'Try leaving out the space before the C and run it.
  279. X=INSTR(A$, "IBM")  'Remember the INSTR function?
  280. PRINT A$
  281. MID$(A$,X)=B$       'Replace "IBM PCs" with " Clones"
  282. PRINT A$
  283. END
  284. 'Only MID$ can be used in this way, not LEFT$ or RIGHT$.
  285. 'See "MID$ Statement" in the manual for other restrictions.
  286. '(PgDn)
  287. 'page 14                     Other String Commands
  288.  
  289. 'INSTR and LEN
  290.  
  291.      example8:
  292.      A$="12345abc345xyz": PRINT "A$=" A$
  293.      B$="345": PRINT "B$=" B$
  294. 'INSTR is the character number in the first string where the second string
  295. '  begins. If the second string is not found, INSTR is 0.
  296.      X=INSTR(A$, B$)
  297.      PRINT "Starting @ char. #" X, MID$(A$, X, 6)
  298. 'You can specify a point for the search for a match to start:
  299.      Y=INSTR( X+1, A$, B$)
  300. '              -the starting point is the start of the first "345" + one
  301.      PRINT "Starting @ char. #" Y, MID$(A$, Y)
  302.  
  303. 'LEN returns the length of a string:
  304.      PRINT "A$ is" LEN(A$) "char. long"
  305.      END
  306.  
  307.  
  308. '                               end of lesson 2
  309.  
  310.  
  311.  
  312. 'end of file.  Press PgUp.
  313.  
  314.